import 'package:flutter/material.dart';
import 'package:gd_assistant/api/apiHandler.dart';
import 'package:gd_assistant/model/healthRecordModel.dart';
import 'package:gd_assistant/page/public/colors.dart';
import 'package:gd_assistant/page/public/publicView.dart';
import 'package:gd_assistant/page/public/publicItem.dart';
import 'package:gd_assistant/page/public/themeUtils.dart';
import 'package:gd_assistant/page/user/patientInfoPage.dart';
import 'package:gd_assistant/page/user/userAppointment.dart';
import 'package:gd_assistant/page/user/userArchivesPage.dart';
import 'package:gd_assistant/util/customRoute.dart';

class healthRecordPage extends StatefulWidget {
  healthRecordPage({Key key, this.userid, this.orderid, this.userName, this.isExpire = false}):super(key: key);
  String userid = '';
  String orderid = '';
  String userName = '';
  bool isExpire; //是否过期
  @override
  _healthRecordPageState createState() => _healthRecordPageState();
}

class _healthRecordPageState extends State<healthRecordPage> {
  bool isDark = false;
  List<HealthRecordModel> _dataList = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    loadData();
  }

  void loadData() async {
    List<HealthRecordModel> dataList = await apiHandler.getHealthRecordList(widget.userid, widget.orderid);
    _dataList = dataList;
    setState(() {});
  }
  @override
  Widget build(BuildContext context) {
    final EdgeInsets padding = MediaQuery.of(context).padding;
    isDark = ThemeUtils.isDark(context);
    return Scaffold(
      appBar: AppBar(
        title: Text("健康档案"),
        elevation: 0
      ),
      body: Container(
        child: Column(
          children: <Widget>[
            Expanded(
              flex: 1,
              child: ListView.builder(
                itemBuilder: (BuildContext cext, int index){
                  if (index == 0) {
                    return GestureDetector(
                              onTap: (){
                                  Navigator.of(context).push(
                                    CustomRoute(patientInfoPage(userId: widget.userid, orderId: widget.orderid, 
                                    callBack: (String name){setState(() {
                                      widget.userName = name;
                                    });},))
                                  );  
                              },
                              child: Container(
                                height: 60,
                                color: isDark ? Colours.dark_bg_gray : Colors.white,
                                child: Padding(
                                  padding: EdgeInsets.fromLTRB(20, 0, 15, 0),
                                  child:  Row(
                                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                    children: <Widget>[
                                      Text("就诊人信息", style: TextStyle(fontSize: 18, color: isDark ? Colours.dark_text : Colours.text)),
                                      Row(
                                        children: <Widget>[
                                          Padding(
                                            padding: EdgeInsets.only(right: 5),
                                            child: Text(widget.userName, style: TextStyle(fontSize: 16, color: isDark ? Colours.dark_text : Colours.text)),
                                          ),
                                          Icon(Icons.arrow_forward_ios, size: 15, color: Colors.grey),
                                        ],
                                      )
                                    ],
                                  ),
                                )
                              ),
                            );
                  }
                  return healthRecordItem(
                          health: _dataList[index-1],
                          onTap: (){
                            Navigator.of(context).push(
                              CustomRoute(userArchivesPage(userId: widget.userid,orderId: widget.orderid,userName: widget.userName,healthRecord: _dataList[index-1],callBack: (){ 
                                loadData(); 
                              },))
                            ); 
                          }
                        );
                },
                itemCount: _dataList.length + 1,
              ),
            ),
            Offstage(
              offstage: widget.isExpire,
              child: Container(
                padding: EdgeInsets.only(bottom: padding.bottom),
                width: double.infinity,
                height: 65 + padding.bottom,
                color: isDark ? Colours.dark_bg_color : Colors.white,  
                child: ZLButton(
                  title: "新增档案",
                  onTap: (){
                    Navigator.of(context).push(
                      CustomRoute(userArchivesPage(userId: widget.userid,orderId: widget.orderid,userName: widget.userName, callBack: (){loadData();}))
                    );      
                  },
                ),
              ),
            )
          ],
        )
      ),
    );
  }
}